mini.parse();

function AgentHandler() {
};

AgentHandler.instance = new AgentHandler();

AgentHandler.isQueryBtnExpended = false;

//获得全局的代理商数据表格对象
AgentHandler.agentGrid = mini.get("agentGrid");

//加载表格数据
AgentHandler.agentGrid.load();

//类型状态数据
AgentHandler.typeStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("baiState").setData(AgentHandler.typeStateData);

/**
 * 根据用户权限展示功能按钮
 */
AgentHandler.loadToolbar = function() {
	var array = new Array("agent_list_add", "agent_list_edit", "agent_list_del", "agent_list_query", "agent_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
AgentHandler.loadToolbar();

/**
 * 弹出添加代理商窗口
 */
AgentHandler.addAgent = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(334, 170, "添加代理商");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("agentForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.AgentFormHandler.passData(data);
	},function(action){
		AgentHandler.agentGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑代理商窗口
 */
AgentHandler.editAgent = function() {
	var rowData = AgentHandler.agentGrid.getSelected();
	var rowsData = AgentHandler.agentGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(334, 170, "编辑代理商[<font color='blue'>"	+ rowData.baiName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("agentForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", baiId: rowData.baiId};
            iframe.contentWindow.AgentFormHandler.passData(data);
		},function(action){
			AgentHandler.agentGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除代理商资源
 */
AgentHandler.delAgent = function() {
	var rowsData = AgentHandler.agentGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].baiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].baiId;
					
					// 判断选择的代理商是否已拥有代理区域
					if(AgentHandler.getBdrBybai(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在区域信息关联，请检查后重试！");
						return;	
					}
					
					AgentHandler.agentGrid.loading();
					
                    $.ajax({
                        url: __root + "/delAgentsByIds.do",
                        data: {
				        	baiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		AgentHandler.agentGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	AgentHandler.agentGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 判断选择的代理商是否已拥有代理区域
 * @param {} baiIds
 * @return {}
 */
AgentHandler.getBdrBybai = function(baiIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBdrBybai.do?baiIds=" + baiIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新代理商状态
 */
AgentHandler.updateAgentState = function() {
	if (!MiniuiTools.hasPurview("agent_list_edit")) {
		return;
	}
	var rowData = AgentHandler.agentGrid.getSelected();
	var baiId = rowData.baiId;	
	AgentHandler.agentGrid.loading();
	
    $.ajax({
        url: __root + "/updateAgentStateById.do",
        data: {
        	baiId: baiId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            AgentHandler.agentGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	AgentHandler.agentGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询代理商面板
 */
AgentHandler.queryAgent = function() {
	if (AgentHandler.isQueryBtnExpended) {
		AgentHandler.isQueryBtnExpended = false;
		mini.get("agent_list_query").setText("展开查询");
		mini.get("agent_list_query").setIconCls("icon-btn-expand");
	} else {
		AgentHandler.isQueryBtnExpended = true;
		mini.get("agent_list_query").setText("折叠查询");
		mini.get("agent_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryAgentDiv").toggle(500);
    setTimeout(function(){mini.get("agentPanel").doLayout();},600);
};

/**
 * 执行查询代理商的方法
 */
AgentHandler.doQueryAgent = function(){
	var baiName = mini.get("baiName").getValue();
	var baiState = mini.get("baiState").getValue();
	
    AgentHandler.agentGrid.load({baiName: baiName, baiState: baiState});
};

/**
 * 刷新页面数据及布局
 */
AgentHandler.afresh = function(){
	AgentHandler.doQueryAgent();
	mini.get("agentPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
AgentHandler.agentGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "baiState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='AgentHandler.updateAgentState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='AgentHandler.updateAgentState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

